﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.DataModel.Users;
using NPMonitor.Implementation;
using NPMonitor.Web.UserControls;

namespace NPMonitor.Web
{
    public partial class UserManagement : BasePage
    {
        #region 属性
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize
        {
            get { return ctPager.PageSize; }
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageIndex
        {
            get { return ctPager.CurrentPageIndex; }
        }

        /// <summary>
        /// 获取PagerControl
        /// </summary>
        public PagerControl ListPagerControl { get { return this.ctPager; } }


        public int UserCount
        {
            get
            {
                return Convert.ToInt32(ViewState["UserCount"]);
            }
            set
            {
                ViewState["UserCount"] = value;
            }
        }

        public bool PhEdit { 
            get{
                return phUserInfo.Visible;
            }
            set
            {
                phUserInfo.Visible = value;
            }
        }

        #endregion
        #region 方法
        public override string FunctionName
        {
            get { return FunctionEnum.Users.ToString(); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ctPager.PageChange += new PagerControl.PageRefresh(UserDataBind);
            if (!IsPostBack)
            {
                UserDataBind(0);
            }
        }

        public void UserDataBind(int pageIndex)
        {
            if (pageIndex == -1)
            {
                pageIndex = ctPager.CurrentPageIndex;
            }
            UserCount = StoreManager.Current.UserStore.GetUsersCounts();
            ctPager.IntialProperties(UserCount);
            List<Users> list = new List<Users>();
            if (pageIndex > ctPager.TotalPage - 1)
            {
                pageIndex = ctPager.TotalPage - 1;
            }
            list = StoreManager.Current.UserStore.GetAllUsers(pageIndex, PageSize);
            rpUserList.DataSource = list;
            rpUserList.DataBind();
        }

        protected void imgRefresh_OnClick(object sender, EventArgs e)
        {
            UserDataBind(ctPager.CurrentPageIndex);
        }

        protected void lbtnCreateUser_Click(object sender, EventArgs e)
        {
            cleanUserControl();
            PhEdit = true;
        }

        protected void cleanUserControl()
        {
            ctUserInfo.UserId = 0;
            ctUserInfo.UserName = "";
            ctUserInfo.Account = "";
            ctUserInfo.Mail = "";
            ctUserInfo.status = UserStatus.Active.ToString();
        }

        protected void rpUserList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                int useId = Convert.ToInt32(e.CommandArgument);
                if (e.CommandName.Trim() == "DeleteUser")
                {
                    StoreManager.Current.UserStore.DeleteUser(useId);
                }

                if (e.CommandName.Trim() == "DisableUser")
                {
                    StoreManager.Current.UserStore.ChangeUserStatus(useId, UserStatus.Disable);
                }

                if (e.CommandName.Trim() == "ActiveUser")
                {
                    StoreManager.Current.UserStore.ChangeUserStatus(useId, UserStatus.Active);
                }

                if (e.CommandName.Trim() == "EditUser")
                {
                    var user = StoreManager.Current.UserStore.GetUserInfoById(useId);
                    if (user != null)
                    {
                        ctUserInfo.UserId = useId;
                        ctUserInfo.UserName = user.UserName;
                        ctUserInfo.Account = user.Account;
                        ctUserInfo.Mail = user.Mail;
                        ctUserInfo.status = user.Status;
                    }
                    PhEdit = true;  
                }
                UserDataBind(ctPager.CurrentPageIndex);
            }
        }

        protected void rpUserList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Users userInfo = (Users)e.Item.DataItem;
            LinkButton lbtnDisableUser = e.Item.FindControl("lbtnDisable") as LinkButton;
            LinkButton lbtnbActiveUser = e.Item.FindControl("lbtnActive") as LinkButton;
            if (userInfo.Status == "Active")
            {
                lbtnDisableUser.Visible = true;
                lbtnbActiveUser.Visible = false;
            }

            if (userInfo.Status == "Disable")
            {
                lbtnDisableUser.Visible = false;
                lbtnbActiveUser.Visible = true;
            }

        }
    }
        #endregion
}